<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/HtmlElement.php';

/**
 * Helper to generate a mailto link element
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3mailto extends Zend_View_Helper_HtmlElement
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates a mailto link.
	 *
     * If no arguments are passed, returns a Tx_Zview_Helper_T3mailto object.
     *
	 * @param string $email
	 * @param string $name
	 * @param array $attribs
	 * @return string|Tx_Zview_Helper_T3mailto
	 */
	public function t3mailto($email='', $name='', array $attribs = array())
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->link($email, $name, $attribs);
	}

	/**
	 * Generates a mailto link.
	 *
	 * @param string $email
	 * @param string $name
	 * @param array $attribs
	 * @return string
	 */
	public function link($email, $name='', array $attribs = array())
	{
		$parts = $this->view->getPi()->cObj->getMailTo($email, $name);
		if(!empty($parts)) {
			return sprintf('<a href="%s"%s>%s</a>', $parts[0], $this->_htmlAttribs($attribs), $parts[1]);
		}
		return '';
	}

	/**
	 * Generates a mailto href.
	 *
	 * Use this for the href attribute of a link tag.
	 *
	 * @param string $email
	 * @return string
	 */
	public function href($email)
	{
		$parts = $this->view->getPi()->cObj->getMailTo($email, $email);
		if(!empty($parts)) {
			return $parts[0];
		}
		return '';
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3mailto.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3mailto.php']);
}
